package org.cos.verifier.ui;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Event;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import org.cos.verifier.process.Rule;
import org.cos.verifier.process.RuleManager;


public class RuleEditListDlg extends Frame {
	RuleEditListDlg me;
	Label title;
	Panel ctrl_panel;
	List rule_list;
	Panel rules_panel;
	ScrollPane list_panel_wrap;
	Button edit_btn, add_btn, remove_btn, load_btn, dump_btn, export_btn;
	Vector chks = null;
	public String crr_name = null;
	Rule crr_rule;
	TextArea rule_describ ;
	public boolean is_edit = false;
	public Frame parent =null;
	
	public RuleEditListDlg() {
		this.me = this;
		chks = new Vector();
		rule_describ = new TextArea();
		me.setLayout(new BorderLayout());
		title = new Label("Cos-Verifier 规则管理器");
		me.add(title, BorderLayout.NORTH);
		rule_list = new List();
		rule_list.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				e.getStateChange();
				crr_name = rule_list.getSelectedItem();
				rule_describ.setText(RuleManager.get_inst().get_rule(crr_name).describ);
				edit_btn.setEnabled(true);
				remove_btn.setEnabled(true);
			}
		});
		
		
		list_panel_wrap = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
		list_panel_wrap.add(rule_list);		
		reload();
		rules_panel = new Panel();
		rules_panel.setLayout(new GridLayout(2,1));
		rules_panel.add(list_panel_wrap);
		rules_panel.add(rule_describ);
		me.add(rules_panel, BorderLayout.CENTER);
		
		ctrl_panel = new Panel();
		ctrl_panel.setLayout(new GridLayout(2,3));
		
		edit_btn = new Button("编辑");
		edit_btn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				is_edit = true;
				RuleEditorDlg edit_dlg = new RuleEditorDlg();
				edit_dlg.display(me);
			}
		});
		edit_btn.setEnabled(false);

		
		add_btn = new Button("导入");
		add_btn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				FileDialog fd = new FileDialog(me, "选择文件");
				fd.setVisible(true);
				String path = fd.getDirectory();
				String name = fd.getFile();
				if (path == null || name == null) {
					return;
				}
				fd.dispose();
			}
		});
		remove_btn = new Button("删除");
		remove_btn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if (crr_name.length()!=0)
					RuleManager.get_inst().remove_rule(crr_name);
				reload();
			}
		});
		remove_btn.setEnabled(false);

		load_btn = new Button("加载");
		load_btn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				RuleManager.get_inst().load();
				reload();
			}
		});
		
		dump_btn = new Button("转储");
		dump_btn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				RuleManager.get_inst().dump();
			}
		});
		
		export_btn = new Button("导出");
		export_btn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {

			}
		});	
		
		ctrl_panel.add(edit_btn);
		ctrl_panel.add(add_btn);
		ctrl_panel.add(remove_btn);
		ctrl_panel.add(load_btn);
		ctrl_panel.add(dump_btn);
		ctrl_panel.add(export_btn);

		me.add(ctrl_panel, BorderLayout.SOUTH);
		me.setSize(400, 500);
		
		me.addWindowListener(new FrameHandler());
	}
	
	public void reload(){
		rule_list.removeAll();
		Hashtable rs = RuleManager.get_inst().get_all_rule();
		for (Enumeration ex = rs.elements(); ex.hasMoreElements(); ){
			crr_rule = (Rule) ex.nextElement();
			String rule_title = crr_rule.name;
			rule_list.add(rule_title);
		}
		is_edit = false;
	}
	
	public void display(Frame parent){
		this.parent = parent;
		me.setVisible(true);
	}
	
	class FrameHandler extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			me.dispose();
			new RuleMgrDlg().display();
		}
	}
}
